package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.utils.common.exception.CatchCustomException;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("ALL")
public class AdChannelServiceImpl implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {

        if(channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        channelDto.checkParam();


        Page<AdChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());

        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(channelDto.getName()!=null && !"".equals(channelDto.getName()),AdChannel::getName,channelDto.getName());

        IPage<AdChannel> iPage = adChannelMapper.selectPage(page,lambdaQueryWrapper);

        ResponseResult responseResult =  new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)iPage.getTotal());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }


    @Override
    public ResponseResult save(AdChannel adChannel) {

        if(adChannel==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adChannel.setCreatedTime(new Date());
        adChannelMapper.insert(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findById(Integer id) {
        if(id==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = adChannelMapper.selectById(id);
        return new ResponseResult().ok(0,adChannel,"查询成功");
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        if(adChannel==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ResponseResult responseResult = this.findById(adChannel.getId());
        if(responseResult.getData()==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        adChannelMapper.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id){
        if(id==null){

            CatchCustomException.catchs(20001,"Id不能为空");
        }

        ResponseResult responseResult = this.findById(id);
        if(responseResult.getData()==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        AdChannel adChannel = (AdChannel) responseResult.getData();

        if(adChannel.getStatus()){

         return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deletes(Integer[] ids) {
        if(ids==null || ids.length==0){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannelMapper.deleteBatchIds(Arrays.asList(ids));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findAll() {
       LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(AdChannel::getStatus,true);
       lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        List<AdChannel> list=adChannelMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }


}
